package org.xmlParser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.SortedMap;
import java.util.TreeMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;




public class typeOf {
	public static HashMap<String ,Integer> global_dict;
	public HashMap<String,Integer> local_dict;
	public String url;
	public String query;
	public ArrayList<String> contentSimilar;
	
	public void setquery(String query){
		this.query=query;
		this.url="http://pixqr01.img.search.bf1.yahoo.com:4080/?query="+url.replace(' ', '+')+"&select=all%28group%28conceptkeywords%29order%28-count%28%29%29each%28max%281%29+output%28count%28%29%29%29%29&restrict=pix";
	}
	
	public void setUrl(String url){
		
		this.url=url;
	}
	
	
	
	public void init_local(){
		local_dict=new HashMap<String,Integer>();
		Document doc;
		try{
		 doc=xmlReader.createDocUrl(url);
		
		
		NodeList n1=doc.getElementsByTagName("group");
		
		
		for(int i=0;i<n1.getLength();i++){
			String type=xmlReader.getAtributeValue("string", (Element)n1.item(i), "id", "type");
			int count=Integer.parseInt(xmlReader.getAtributeValue("count()", (Element)n1.item(i), "output", "label"));
			local_dict.put(type, count);
			
		}
		NodeList n2=doc.getElementsByTagName("result");
		Element e1=(Element)n2.item(0);
		local_dict.put("total-count", Integer.parseInt(e1.getAttribute("total-hit-count")));
		}catch(Exception e){}

	}
	
	public static void init_global(){
		global_dict=new HashMap<String,Integer>();
		Document doc;
		
		try{
			doc=xmlReader.createDocUrl("http://pixqr01.img.search.bf1.yahoo.com:4080/?query=lang:en&select=all%28group%28conceptkeywords%29order%28-count%28%29%29each%28max%281%29+output%28count%28%29%29%29%29&restrict=pix");
			NodeList n1=doc.getElementsByTagName("group");
			
		
			for(int i=0;i<n1.getLength();i++){
				String type=xmlReader.getAtributeValue("string", (Element)n1.item(i), "id", "type");
				int count=Integer.parseInt(xmlReader.getAtributeValue("count()", (Element)n1.item(i), "output", "label"));
				global_dict.put(type, count);

	
		}
			NodeList n2=doc.getElementsByTagName("result");
			Element e1=(Element)n2.item(0);
			global_dict.put("total-count", Integer.parseInt(e1.getAttribute("total-hit-count")));
			
			
			
			

			
		}catch(Exception e){}
	}
	
	
	public void predictTopN(int n){
		contentSimilar=new ArrayList<String>();
		PriorityQueue q=new PriorityQueue<Float>();
		SortedMap<Float, String> type=new TreeMap();
		float r=global_dict.get("total-count")/local_dict.get("total-count");
		for(String s:local_dict.keySet()){
			if(global_dict.containsKey(s)){
				type.put(local_dict.get(s)/global_dict.get(s)*r,s);
			}
			else
				type.put(local_dict.get(s)*r,s);
			
		}
		
		 Comparator<Float> valueComparator = new Comparator<Float>() {
		        @Override public int compare(Float s1, Float s2) {
		        	if(s1>s2)
		        		return 0;
		        	else
		        		return 1;
		            
		        }           
		    };

		   ArrayList<Float> keyset=new ArrayList<Float>(); 
		
		
		Iterator itr=type.keySet().iterator();
		while(itr.hasNext()){
			Object key=itr.next();
			keyset.add(Float.parseFloat(key.toString()));
			}
		
		Collections.sort(keyset, valueComparator);
		
		for(int i=0;i<keyset.size()&&i<n;i++){
			
			contentSimilar.add(type.get(keyset.get(i)));
		}
		
		
		
	}
	public  void removException(){
		ArrayList<String> abc=new ArrayList<String>();
		abc.add("Building Exterior");
		
		for(int i=0;i<abc.size();i++){
			int loc=this.contentSimilar.indexOf(abc.get(i));
			if(loc<0)continue;
			else
				contentSimilar.remove(abc.get(i));
		}
	}
	
	public String predictIntent(String query) throws Exception{
		
		url="http://qrs04.image.search.ac4.yahoo.com:4080/?query="+query.replace(' ', '+')+"&custid=imagesearch&hits=1&imageworthy=true&thumbsize=200&tracelevel=9&nocache&template=classification";
		
		String arr[]=new String[5];
		
			
			String text=xmlReader.xmlFromUrl(url);
			if(text.indexOf("categories")>0){
			arr=text.split("categories\":");			
			
			
			

		
		
			
			int a =arr[1].indexOf(']');
			
			return (arr[1].substring(2, a-1));}
			
		else 
			return null;

		
	}
	
	

}
